package mtm.ui;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import mtm.constants.Constants;
import mtm.ui.panel.CalendarPanel;
import mtm.ui.panel.MonthPanel;
import mtm.ui.panel.WeekPanel;

public class MainFrame extends JFrame {

	private static MainFrame instance = null;
	
	private WeekPanel weekPanel;
	private MonthPanel monthPanel;
	private JTabbedPane tab;
	
	
	/**
	 * Return singleton instance of MainFrame
	 * @return
	 */
	public static MainFrame getInstance() {
		return instance == null ? new MainFrame() : instance;
	}


	/**
	 * Constructor
	 */
	private MainFrame() {
		super("Quản lý thời gian");
		instance = this;
		
		// calendar panel
		CalendarPanel calendarPanel = CalendarPanel.getInstance();
		
		// week frame
		weekPanel = WeekPanel.getInstance();
		
		// month panel
		monthPanel = MonthPanel.getInstance();
		
		// tab
		tab = new JTabbedPane();
		tab.add("Tuần", weekPanel);
		tab.add("Tháng", monthPanel);
		
		// listener for tab
		tab.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent arg0) {
				// display the first week of selected month
				if (isWeekView()) {
//					MtmMonth selectedMonth = CalendarPanel.getInstance().getSelectedMonth();
//					weekPanel.displayJobsInWeek(new MtmDate(selectedMonth.getYear(), selectedMonth.getMonth(), 1));
				} 
				// display the selected month
				else {
					monthPanel.displayJobsInMonth(CalendarPanel.getInstance().getSelectedMonth());
				}
			}
		});
		
		// set layout
		setLayout(new BorderLayout());
		JPanel westPanel = new JPanel();
		westPanel.setLayout(new GridLayout(3, 1));
		westPanel.add(calendarPanel);
		add(westPanel, BorderLayout.WEST);
		add(tab, BorderLayout.CENTER);
		
		// other settings
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(false);
		setVisible(true);
		setIconImage(new ImageIcon(Constants.PROGRAM_ICON).getImage());
		setSize(WeekPanel.WIDTH_OF_CELL * 7 + 2 * WeekPanel.BORDER_WIDTH + 250, 
				24 * WeekPanel.HEIGHT_OF_CELL + 2 * WeekPanel.BORDER_HEIGHT + 100);
	}
	
	
	/**
	 * Set view mode to Week view or Month view
	 * @param mode
	 */
	public void setJobView(int mode) {
		switch (mode) {
		case Constants.WEEK_VIEW:
			tab.setSelectedComponent(weekPanel);
			break;
			
		case Constants.MONTH_VIEW:
			tab.setSelectedComponent(monthPanel);
			break;
		}
	}
	
	
	/**
	 * Check current selected tab is WeekPanel or not ?
	 */
	public boolean isWeekView() {
		return tab.getSelectedComponent().equals(weekPanel);
	}
	
	
	/**
	 * Main
	 * @param args
	 */
	public static void main(String[] args) {
		MainFrame.getInstance();
	}
}
